/**
 * 向上转型的三种方式
 * 1.直接赋值
 * 2.方法传参的方式
 * 3.返回值
 *
 * 重写注意事项
 *1.被 privite static final 修饰的方法不能被重写
 *2.子类访问修饰限定符的权限必须>=父类 例如 父类是 protected  子类只能是 protected 或public
 *3.方法的返回值可以不同，但必须构成父子类
 *4.构造方法不能重写
 */
class  Animal{
    public String name;
    public int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Animal eat(){
        System.out.println(name + "吃东西!");
        return null;
    }

    //这也是重写
    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}



class Dog extends Animal{
    public Dog(String name, int age) {
        super(name, age);
    }
    public void wangWang(){
        System.out.println(name + "汪汪叫!");
    }
    public Dog eat(){
        System.out.println(name + "吃狗粮!");
        return null;
    }

}



class Bird extends Animal{
    public Bird(String name, int age) {
        super(name, age);
    }
    public void fly(){
        System.out.println(name + "会飞!");
    }

    public Bird eat(){
        System.out.println(name + "吃虫子!");
        return null;
    }

}


public class Main {
    public static void main(String[] args) {
        Animal animal1 = new Dog("小狗",5);
        //不安全 向下转型（父类对象需要引用子类方法的时候）
        Dog dog = (Dog) animal1;//int i=(int) float j;
       // ((Dog) animal1).wangWang();
        dog.wangWang();


       Animal animal2 = new Bird("小鸟",5);
        if(animal2 instanceof Dog){
            Dog dog1 = (Dog) animal2 ;
            dog1.wangWang();
        }else {
            System.out.println("不能汪汪叫");
        }
    }
    public static void main2(String[] args) {
        Animal animal1 = new Dog("小狗",5);
        //这里调用了Dog里的方法是因为eat方法被重写了 这个过程叫动态绑定（运行时绑定）
        animal1.eat();
    }
    public static void main1(String[] args) {
        //1.直接赋值 父类引用 引用了子类对象
        Animal animal1 = new Dog("小狗",5);
        Animal animal2 = new Bird("小鸟",5);

        //2.方法传参
        Dog dog = new Dog("小狗",5);
        Bird bird = new Bird("小鸟",5);
        func1(dog);
        func1(bird);

        //3.返回值
        Animal animal = func2();
        System.out.println(animal.toString());
    }


    public static Animal func2(){
        return new Bird("大鸟",5);
    }
    public static void func1(Animal animal){

    }
}